package com.nd.service;

import cn.hutool.core.date.DateTime;
import com.nd.dao.CategoryMapper;
import com.nd.pojo.bean.BrandExample;
import com.nd.pojo.bean.Category;
import com.nd.pojo.bean.CategoryExample;
import com.nd.pojo.bo.CategoryCreateBO;
import com.nd.pojo.bo.CategoryDeleteBO;
import com.nd.pojo.bo.CategoryUpdateBO;
import com.nd.pojo.vo.CateGoryL1VO;
import com.nd.pojo.vo.CategoryCreateVO;
import com.nd.pojo.vo.CategoryListAllVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-05 11:28
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CateGoryL1VO> categoryL1() {
        List<CateGoryL1VO> cateGoryL1VOList = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        for (Category category : categoryList) {
            CateGoryL1VO cateGoryL1VO = new CateGoryL1VO();
            cateGoryL1VO.setValue(category.getId());
            cateGoryL1VO.setLabel(category.getName());
            cateGoryL1VOList.add(cateGoryL1VO);
        }
        return cateGoryL1VOList;
    }

    @Override
    public List<CategoryListAllVO> categoryList() {
        List<CategoryListAllVO> categoryListAllVOS = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        for (Category category : categoryList) {
            CategoryListAllVO categoryListAllVO = new CategoryListAllVO();
            categoryListAllVO.setId(category.getId());
            categoryListAllVO.setName(category.getName());
            categoryListAllVO.setKeywords(category.getKeywords());
            categoryListAllVO.setDesc(category.getDesc());
            categoryListAllVO.setIconUrl(category.getIconUrl());
            categoryListAllVO.setPicUrl(category.getPicUrl());
            categoryListAllVO.setLevel(category.getLevel());

            CategoryExample categoryExampleChildren = new CategoryExample();
            CategoryExample.Criteria criteriaChildren = categoryExampleChildren.createCriteria();
            criteriaChildren.andPidEqualTo(category.getId()).andDeletedEqualTo(false);
            List<Category> categoryListChildren = categoryMapper.selectByExample(categoryExampleChildren);

            List<CategoryListAllVO.ChildrenBean> childrenBeanList = new ArrayList<>();
            for (Category child : categoryListChildren) {
                CategoryListAllVO.ChildrenBean childrenBean = new CategoryListAllVO.ChildrenBean();
                childrenBean.setId(child.getId());
                childrenBean.setName(child.getName());
                childrenBean.setKeywords(child.getKeywords());
                childrenBean.setDesc(child.getDesc());
                childrenBean.setIconUrl(child.getIconUrl());
                childrenBean.setPicUrl(child.getPicUrl());
                childrenBean.setLevel(child.getLevel());
                childrenBeanList.add(childrenBean);
            }
            categoryListAllVO.setChildren(childrenBeanList);
            categoryListAllVOS.add(categoryListAllVO);
        }
        return categoryListAllVOS;
    }

    @Override
    public CategoryCreateVO categoryCreate(CategoryCreateBO categoryCreateBO) {
        CategoryCreateVO categoryCreateVO = new CategoryCreateVO();
        Category category = new Category();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        byte max = categoryMapper.findMaxSortOrder();
        DateTime time = DateTime.now();
        category.setName(categoryCreateBO.getName());
        category.setKeywords(categoryCreateBO.getKeywords());
        category.setDesc(categoryCreateBO.getDesc());
        category.setPid(categoryCreateBO.getPid());
        category.setIconUrl(categoryCreateBO.getIconUrl());
        category.setPicUrl(categoryCreateBO.getPicUrl());
        category.setLevel(categoryCreateBO.getLevel());
        category.setSortOrder(++max);
        category.setAddTime(time);
        category.setUpdateTime(time);
        category.setDeleted(false);
        categoryMapper.categoryCreate(category);
        CategoryExample categoryExample2 = new CategoryExample();
        CategoryExample.Criteria criteria2 = categoryExample2.createCriteria();
        criteria2.andSortOrderEqualTo(max);
        List<Category> categories = categoryMapper.selectByExample(categoryExample2);
        int id = 0;
        for (Category category1 : categories) {
            id = category1.getId();
        }
        categoryCreateVO.setId(id);
        categoryCreateVO.setName(categoryCreateBO.getName());
        categoryCreateVO.setKeywords(categoryCreateBO.getKeywords());
        categoryCreateVO.setDesc(categoryCreateBO.getDesc());
        categoryCreateVO.setPid(categoryCreateBO.getPid());
        categoryCreateVO.setIconUrl(categoryCreateBO.getIconUrl());
        categoryCreateVO.setPicUrl(categoryCreateBO.getPicUrl());
        categoryCreateVO.setLevel(categoryCreateBO.getLevel());
        categoryCreateVO.setAddTime(time);
        categoryCreateVO.setUpdateTime(time);
        return categoryCreateVO;
    }


    //公网存在的问题：一级类有二级类的情况也能转成二级类；一级类转到自己的二级类会导致其变成二级类，从而不会再显示
    @Override
    public int categoryUpdate(CategoryUpdateBO categoryUpdateBO) {
        if ("L2".equals(categoryUpdateBO.getLevel())) {
            //转二级前有子类，则报错
            if (categoryUpdateBO.getChildren().size() != 0) {
                return 0;
            }
            //禁止一级转成自己的子类
            if (categoryUpdateBO.getId().equals(categoryUpdateBO.getPid())) {
                return 2;
            } else {
                Category category = Category.builder().id(categoryUpdateBO.getId()).name(categoryUpdateBO.getName()).keywords(categoryUpdateBO.getKeywords()).desc(categoryUpdateBO.getDesc()).pid(categoryUpdateBO.getPid()).iconUrl(categoryUpdateBO.getIconUrl()).picUrl(categoryUpdateBO.getPicUrl()).level(categoryUpdateBO.getLevel()).updateTime(DateTime.now()).build();
                categoryMapper.categoryUpdate(category);
                return 1;
            }
        } else {
            //一级转一级或者二级转一级
            Category category = Category.builder().id(categoryUpdateBO.getId()).name(categoryUpdateBO.getName()).keywords(categoryUpdateBO.getKeywords()).desc(categoryUpdateBO.getDesc()).pid(0).iconUrl(categoryUpdateBO.getIconUrl()).picUrl(categoryUpdateBO.getPicUrl()).level(categoryUpdateBO.getLevel()).updateTime(DateTime.now()).build();
            categoryMapper.categoryUpdate2(category);
            return 1;
        }
    }

    @Override
    public void categoryDelete(CategoryDeleteBO categoryDeleteBO) {
        Integer id = categoryDeleteBO.getId();
        categoryMapper.deleteCategory(id);
    }
}
